// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
    if(event.flag=="willSign"){
        try {
            const result = await cloud.openapi.subscribeMessage.send({
                "touser": event.openId,
                "page": '/miniprogram/pages/projectDetail/projectDetail?id='+event.projectId,
                "lang": 'zh_CN',
                "data": {
                    "thing1": {
                        "value": event.projectName
                    },
                    "thing6": {
                        "value": event.teamNickname
                    },
                    "thing7": {
                        "value": event.userName
                    },
                    "phone_number9": {
                        "value": event.phoneNum
                    },
                    "time8": {
                        "value": event.dateTime
                    }
                },
                "templateId": '6OXlbXtKhnrpQDhjf-5oqQDh7O_-eavpqnNMwK-eXm8',
                "miniprogramState": 'developer'
            })
            console.log(result)
            return result.errCode
        } catch (err) {
            console.log(err)
            return err
        }
    }
    else if(event.flag=="cancelSign"){
        try {
            const result = await cloud.openapi.subscribeMessage.send({
                "touser": event.openId,
                "page": '/miniprogram/pages/projectDetail/projectDetail?id='+event.projectId,
                "lang": 'zh_CN',
                "data": {
                    "thing1": {
                        "value": event.projectName
                    },
                    "thing2": {
                        "value": event.userName
                    },
                    "time3": {
                        "value": event.nowTime
                    },
                    "short_thing4": {
                        "value": event.userName
                    },
                    
                },
                "templateId": 'K9SK2TVjvPDVf4kETDriPWJH9vK608k2lk7t7IWYhh4',
                "miniprogramState": 'developer'
            })
            console.log(result)
            return result.errCode
        } catch (err) {
            console.log(err)
            return err
        }
    }
    else if(event.flag=="judgeProject"){
        try {
            const result = await cloud.openapi.subscribeMessage.send({
                "touser": event.openId,
                "page": '/miniprogram/pages/projectDetail/projectDetail?id='+event.projectId,
                "lang": 'zh_CN',
                "data": {
                    "thing1": {
                        "value": event.projectName
                    },
                    "thing2": {
                        "value": event.condition
                    },
                    "thing9": {
                        "value": event.userName
                    },
                    "thing13": {
                        "value": event.teamNickname
                    },
                    "phone_number14": {
                        "value": event.phoneNum
                    },
                    
                },
                "templateId": 'avzUeNJScbyjO9dhIJ_2_GRfl26zqiX23a00r2b03gw',
                "miniprogramState": 'developer'
            })
            console.log(result)
            return result.errCode
        } catch (err) {
            console.log(err)
            return err
        }
    }
    else if(event.flag=="signInTeam"){
        try {
            const result = await cloud.openapi.subscribeMessage.send({
                "touser": event.openId,
                "page": '/miniprogram/pages/teamDetail/teamDetail?id='+event.teamId,
                "lang": 'zh_CN',
                "data": {
                    "thing8": {
                        "value": event.teamNickname
                    },
                    "phone_number10": {
                        "value": event.phoneNum
                    },
                    
                },
                "templateId": '6XVtwHnBop88Uo_msDmoa8BTeH-ObKLnbyHa_oMSd2w',
                "miniprogramState": 'developer'
            })
            console.log(result)
            return result.errCode
        } catch (err) {
            console.log(err)
            return err
        }
    }
    else if(event.flag=="cancelSignInTeam"){
        try {
            const result = await cloud.openapi.subscribeMessage.send({
                "touser": event.openId,
                "page": '/miniprogram/pages/teamDetail/teamDetail?id='+event.teamId,
                "lang": 'zh_CN',
                "data": {
                    "thing1": {
                        "value": event.teamNickname
                    },
                    "thing2": {
                        "value": event.userName
                    },
                    "thing5": {
                        "value": event.tips
                    } 
                },
                "templateId": 'jY5O2DaVBn6G4wz5cqnphlgWOe_umUXXoTehMR9enB4',
                "miniprogramState": 'developer'
            })
            console.log(result)
            return result.errCode
        } catch (err) {
            console.log(err)
            return err
        }
    }
}